package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.TabFragmentAdapter;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.fragment.MyFragmnetText;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class TextEditKitActivity extends BaseActivity implements View.OnClickListener {
    private List<String> titles = new ArrayList<>();
    private ViewPager viewPager;
    private TabLayout tablayout;
    private List<Fragment> fragments = new ArrayList<>();
    private TabFragmentAdapter tabFragmentAdapter;
    private ImageView iv_back;
    private List<View> Views = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_texteditkit);
        registerBoradcastReceiver();
        initView();
        initTextTitles();
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionTextEditKitFinish);
        registerReceiver(mListReceiver, myIntentFilter);
    }


    BroadcastReceiver mListReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionTextEditKitFinish:
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mListReceiver);
    }

    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
    }

    private void initTextTitles() {
        String urlsto = "https://api.zhuiyinanian.com/YinianProject/event/GetTextLibraryType";
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        StringRequest req = new StringRequest(urlsto)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.optString("msg").equals("success")) {
                                JSONArray dataArray = jsonObject.optJSONArray("data");
                                for (int i = 0; i < dataArray.length(); i++) {
                                    titles.add(dataArray.optJSONObject(i).optString("textType"));
                                }
                                for (int i = 0; i < titles.size(); i++) {
                                    Fragment fragment = new MyFragmnetText();
                                    Bundle bundle = new Bundle();
                                    bundle.putString("text", titles.get(i));
                                    fragment.setArguments(bundle);
                                    fragments.add(fragment);
//                                    View tabview = getLayoutInflater().inflate(R.layout.tabaltextedit_view, null);
//                                    TextView title = (TextView) tabview.findViewById(R.id.textedit_tab_title);
//                                    title.setText(titles.get(i));
//                                    Views.add(tabview);
                                    tablayout.addTab(tablayout.newTab().setText(titles.get(i)));
                                }
                                tabFragmentAdapter = new TabFragmentAdapter(fragments, titles, getSupportFragmentManager(), TextEditKitActivity.this);
                                viewPager.setAdapter(tabFragmentAdapter);
                                tablayout.setupWithViewPager(viewPager);
                                tablayout.setTabsFromPagerAdapter(tabFragmentAdapter);//给Tabs设置适配器
//                                tablayout.setTabTextColors(getResources().getColor(R.color.yncolor), Color.WHITE);
//                                for (int j = 0; j < titles.size(); j++) {
//                                    tablayout.getTabAt(j).setCustomView(Views.get(j));
//                                }
//                                View thistab1 = Views.get(0);
//                                ImageView thistabiv = (ImageView) thistab1.findViewById(R.id.textedit_tab_iv);
//                                thistabiv.setImageResource(R.drawable.icon_tabtext_edit);
//                                thistab1.setBackgroundResource(R.drawable.yellow_rightbuttom_round);
//                                TextView thistitle = (TextView) thistab1.findViewById(R.id.textedit_tab_title);
//                                thistitle.setTextColor(Color.parseColor("#000000"));
                                viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                                    @Override
                                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                                    }

                                    @Override
                                    public void onPageSelected(int position) {
//                                        for (int i = 0; i < Views.size(); i++) {
//                                            View tabview = Views.get(i);
////                                            ImageView tabiv1 = (ImageView) tabview.findViewById(R.id.textedit_tab_iv);
////                                            tabiv1.setImageResource(-1);
//                                            tabview.setBackgroundResource(R.color.white);
//                                            TextView title = (TextView) tabview.findViewById(R.id.textedit_tab_title);
//                                            title.setTextColor(Color.parseColor("#aeaeae"));
//                                        }
//                                        View thistab = Views.get(position);
////                                        ImageView thistabiv = (ImageView) thistab.findViewById(R.id.textedit_tab_iv);
////                                        thistabiv.setImageResource(R.drawable.icon_tabtext_edit);
//                                        thistab.setBackgroundResource(R.drawable.yellow_rightbuttom_round);
//                                        TextView thistitle = (TextView) thistab.findViewById(R.id.textedit_tab_title);
//                                        thistitle.setTextColor(Color.parseColor("#000000"));
                                    }

                                    @Override
                                    public void onPageScrollStateChanged(int state) {
                                    }
                                });
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }


    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }



}
